<?php defined('BASEPATH') or exit('No direct script access allowed');

class Gallery_m extends MY_Model {

	protected $_table = 'galleries'; 
	
    public function get_gallery($gallery_id)
    {
        $this->db->select('galleries.id, galleries.author, galleries.title, galleries.description, galleries.allow_comments, galleries.votenum, galleries.viewnum, galleries.commentnum, galleries.author_ip, galleries.sticky, galleries.feature, galleries.status, galleries.path, galleries.date_posted, galleries.created, categories.name as category_name, categories.id as category_id, categories.description as category_description');
        $this->db->from($this->_table. ' galleries');
        $this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id');
        $this->db->join('categories', 'galleries_to_categories.category_id = categories.id');
        $this->db->where('galleries.id', $gallery_id);
            
        $query = $this->db->get();
            
        if ($query->num_rows() == 1)
        {
            return $query->row();
        }
    }
    public function get_galleries_by_category($category_id,$limit = array())
    {
        $this->db->select('g.id, g.author, g.title, g.description, g.commentnum, g.allow_comments, g.votenum, g.viewnum, g.commentnum, g.author_ip, g.sticky, g.feature, g.status, g.path, g.date_posted, g.created, c.name, c.description');
        $this->db->from('galleries_to_categories gc');         
        $this->db->join('galleries g' , 'g.id = gc.gallery_id and g.status="published"');
        $this->db->join('categories c', 'gc.category_id = c.id');
        $this->db->where('gc.category_id', $category_id);        
        
        $this->db->order_by('g.votenum', 'DESC');
        $this->db->order_by('gc.id', 'DESC');
        $this->limit($limit);
            
        $query = $this->db->get();
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    }
    public function count_galleries_by_category($category_id)
    {        
        $this->db->from('galleries_to_categories gc');         
        $this->db->join('galleries g' , 'g.id = gc.gallery_id');
        $this->db->join('categories c', 'gc.category_id = c.id');
        $this->db->where('gc.category_id', $category_id);        
        
        $this->db->order_by('gc.id', 'DESC'); 
        
        return $this->db->count_all_results();
    }    
    
    public function edit_gallery_category($gallery_id, $category_id)
    {
       $this->db->where('gallery_id', $gallery_id);
       $this->db->update('galleries_to_categories', array('category_id' => $category_id));
    }
    
    public function get_galleries($limit = array())
    {
        $this->db->select('galleries.id, galleries.author, galleries.title, galleries.description, galleries.commentnum, galleries.allow_comments, galleries.votenum, galleries.viewnum, galleries.commentnum, galleries.author_ip, galleries.sticky, galleries.feature, galleries.status, galleries.path, galleries.date_posted, galleries.created, categories.name, categories.description');
        $this->db->from($this->_table . ' galleries');         
        $this->db->join('galleries_to_categories' , 'galleries.id = galleries_to_categories.gallery_id');
        $this->db->join('categories', 'galleries_to_categories.category_id = categories.id');
        
        $this->db->order_by('votenum', 'DESC');
        $this->db->order_by('id', 'DESC');
        $this->limit($limit);
            
        $query = $this->db->get();
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    }
    public function del_gallery($gallery_id)
    {
        $gallery = $this->get($gallery_id);
       
        if(!empty($gallery))
        {             
           
            //删除作品记录
            $this->db->where('id', $gallery_id);
            $this->db->delete($this->_table);
            //删除作品图片
            @unlink(FCPATH. 'uploads/'. $gallery->path);
            @unlink(FCPATH. 'uploads/'. $gallery->path. '.small.jpg');
            @unlink(FCPATH. 'uploads/'. $gallery->path. '.medium.jpg');
            @unlink(FCPATH. 'uploads/'. $gallery->path. '.large.jpg');
            
            //删除作品分类
            $this->db->where('gallery_id', $gallery_id);
            $this->db->delete('galleries_to_categories');             
        }
    }     
    public function create_gallery($gallery_title = '')
	{
		$current_time = time();
		
		$data = array
					(
						'author' 			=> 0,
						'title' 			=> $this->input->post('title')?$this->input->post('title'):$gallery_title,
						'description' 		=> $this->input->post('description')?$this->input->post('description'):'',
						'allow_comments' 	=> '1',
						'votenum' 			=> 0,
						'viewnum'			=> 0,
						'path'				=> NULL,
						'status'			=> 'published',
						'date_posted' 		=> date('Y-m-d',$current_time),
						'created' 			=> time(),
						'author_ip' 		=> $_SERVER['REMOTE_ADDR']
					);

		$this->db->insert($this->_table, $data);
		$gallery_id =  $this->db->insert_id();
		
//		更新相册路径			
		$gallery_path = date('Y-m/d/Ymd_',$current_time). $gallery_id.'.jpg';
		
		$this->db->where('id', $gallery_id);
		$this->db->update($this->_table, array('path' => $gallery_path));
		return array('gallery_id' => $gallery_id, 'gallery_path' => $gallery_path, 'gallery_url_path' => site_url('uploads/'.$gallery_path));
		
	}
	
	public function add_gallery_to_category($gallery_id, $category_id)
	{
		$data = array
					(
						'gallery_id' => $gallery_id,
						'category_id' => $category_id
					);
					
		$this->db->insert('galleries_to_categories', $data);
	}
    public function delete_gallery($gallery_id)
    {
        $gallery = $this->get_gallery($gallery_id);

        if(!empty($gallery))
        {                              
            $this->db->where('id', $gallery_id);
            $this->db->delete($this->_table);
            
            @unlink(ROOTPATH. '/uploads/'. $gallery['path']);
            @unlink(ROOTPATH. '/uploads/'. $gallery['path']. '.small.jpg');
            @unlink(ROOTPATH. '/uploads/'. $gallery['path']. '.medium.jpg');
            @unlink(ROOTPATH. '/uploads/'. $gallery['path']. '.large.jpg');              
           
            $this->delete_gallery_categories($gallery_id);   
        }
    }
    
    public function delete_gallery_categories($gallery_id)
    {
        $this->db->where('gallery_id', $gallery_id);
        $this->db->delete('galleries_to_categories');
    }
    public function delete_galleries_by_ids($gallery_ids)
    {
        $galleries = $this->get_galleries_by_ids($gallery_ids);
        
        foreach($galleries as $key => $gallery)
        {
        
            @unlink(FCPATH. 'uploads/'. $gallery->path);
            @unlink(FCPATH. 'uploads/'. $gallery->path. '.small.jpg');
            @unlink(FCPATH. 'uploads/'. $gallery->path. '.medium.jpg');
            @unlink(FCPATH. 'uploads/'. $gallery->path. '.large.jpg');
        }
        
        $this->db->where_in('id', $gallery_ids);
        $this->db->delete($this->_table);
            
        $this->db->where_in('gallery_id', $gallery_ids);
        $this->db->delete('galleries_to_categories');  
    }
    public function get_galleries_by_ids($gallery_ids)
    {
        $current_date = time();
        
        $this->db->select('galleries.id, galleries.author, galleries.date_posted, galleries.title, galleries.viewnum, galleries.commentnum, galleries.votenum, galleries.description, galleries.path, galleries.allow_comments, galleries.sticky, galleries.status, galleries.author');
        $this->db->from('galleries');
        $this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id');
        $this->db->join('categories', 'galleries_to_categories.category_id = categories.id');
                  //     
        $this->db->where_in('galleries.id', $gallery_ids);       
        $this->db->order_by('galleries.id', 'DESC');
            
        $query = $this->db->get();
            
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    }
    public function edit_galleries_by_ids($gallery_ids, $data = array())
    {
        $this->db->where_in('id', $gallery_ids);
        $this->db->update($this->_table, $data);
    }
    function count_search($keyword = "" , $searchtype = 1, $category = 0) 
    {        
        if($keyword == "" && $category == 0)
        {
            return 0;
        }
        $this->db->from('galleries');
        //$this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id');                     
        if(trim($keyword) != "")
        {
            if($searchtype== 'id')
            {
                $this->db->where('galleries.id', $keyword);
            }
            elseif($searchtype=='title')
            {
               $this->db->like('galleries.title', $keyword, 'both');
            }   
            else
            {
                 $this->db->like('galleries.description', $keyword, 'both');
            }
        }
                    
        if($category != '0')
        {
             $this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id and galleries_to_categories.category_id = '. $category);
        }
        else
        {              
            $this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id');
        }
        $this->db->join('categories', 'galleries_to_categories.category_id = categories.id');   
        $this->db->order_by('galleries.id', 'DESC'); 
        return $this->db->count_all_results();
    }
    function search($keyword = "" , $searchtype = 1, $category = 0, $limit = array()) 
    {        
        if($keyword == "" && $category == 0)
        {
            return NULL;
        }
        $this->db->select('categories.name, galleries.id, galleries.author, galleries.date_posted, galleries.title, galleries.viewnum, galleries.commentnum, galleries.votenum, galleries.description, galleries.path, galleries.allow_comments, galleries.sticky, galleries.status, galleries.author');
        $this->db->from('galleries');
        //$this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id');  
        if(trim($keyword) != "")
        {
            if($searchtype== 'id')
            {
                $this->db->where('galleries.id', $keyword);
            }
            elseif($searchtype=='title')
            {
               $this->db->like('galleries.title', $keyword, 'both');
            }   
            else
            {
                 $this->db->like('galleries.description', $keyword, 'both');
            }
        }
        
        if($category != 0)
        {
             $this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id and galleries_to_categories.category_id = '. $category);
        }
        else
        {            
             $this->db->join('galleries_to_categories', 'galleries.id = galleries_to_categories.gallery_id');
        } 
        $this->db->join('categories', 'galleries_to_categories.category_id = categories.id');   
        $this->db->order_by('galleries.id', 'DESC');    
        $this->limit($limit);
        return $this->db->get()->result();      
    }
}
